import {
  uploadFeedbackImg,
  addFeedback,
  loginWithCode
} from "../../../api.js"


Page({
  data: {
    imgArr: ["https://miniapp.growthupup.com/app/public/assets/icon/add_pic.png"]
  },
  onLoad: function (options) {

  },
  // 图片上传 支持多图上传
  handleImageUpload() {
    const xp = this
    let errors = []
    wx.chooseImage({
      success: function (res) {
        let promiseArr = []
        for (let i = 0, l = res.tempFilePaths.length; i < l; i++) {
          promiseArr.push(new Promise((resolve, reject) => {
            wx.uploadFile({
              url: uploadFeedbackImg,
              filePath: res.tempFilePaths[i],
              name: 'images',
              success(resp) {
                resolve(resp)
              },
              fail(err) {
                errors.push(res.tempFilePaths[i])
              }
            })
          }))
        }
        Promise.all(promiseArr).then(_ => {
          xp.img = _.map(data => data.data)
          xp.setData({
            imgArr: xp.data.imgArr.concat(res.tempFilePaths.filter(f => !errors.includes(f)))
          })
        })
      },
    })
  },
  // input textarea 双向绑定
  inputModel(e) {
    let key = e.target.dataset.key
    let value = e.detail.value
    this.setData({
      [key]: value
    })
  },
  // 提交反馈按钮
  handleFeedBack() {
    let store = getApp().globalData.Store
    let token = store.token
    let business_id = store.business.id
    let images = this.img
    var that = this
    if (!this.checkRequired()) return
    addFeedback(this.data.content, this.data.contact, token, business_id, images).then(res => {
      if (res.data.code == 666) {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/tabBar/personal/personal',
          })
          getApp().endpage(new Date().getTime())
        }, 1000)
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '网络错误，请稍后重试',
          icon: "none"
        })
        that.checkUserInfo()
      }
    })
  },
  /**
   * 检查必填项
   */
  checkRequired() {
    if (!this.data.content) {
      wx.showToast({
        title: "请填写反馈内容",
        icon: "none"
      })
      return false
    } else if (!this.data.contact) {
      wx.showToast({
        title: "请填写联系方式",
        icon: "none"
      })
      return false
    }
    return true
  },
  // 检查用户信息授权
  checkUserInfo() {
    const xp = this;
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userInfo']) {
          xp.setData({
            isAuth: false
          })
          wx.navigateTo({
            url: '/pages/public/shouquan/shouquan',
          })
        } else {
          xp.setData({
            isAuth: true
          })
          wx.getUserInfo({
            success(res) {
              xp.loginWithCode(res.userInfo)
            }
          })
        }
      }
    })
  },
  //登陆
  loginWithCode(userInfo) {
    const xp = this
    wx.login({
      success({
        code
      }) {
        loginWithCode({
          ...userInfo,
          code,
          business_id: getApp().globalData.businessId
        }).then(res => {
          if (res.data.code === 666) {
            getApp().globalData.Store = res.data.data
          }
        })
      }
    })
  },
  onShow() {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  }
})